package com.borya.util;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;

public class SmsUtils {
	private static Logger log = Logger.getLogger(SmsUtils.class);

	public enum SmsType {
		/*** 远盟 ***/
		YUANMENG(0, "【远盟】"),
		/*** 远易站 ***/
		YUANYIZHAN(1, "【远易站】"),
		/*** 信时空 ***/
		XINSHIKONG(2, "【信时空】"),
		/*** 远微商城 ***/
		YUANWEISHANGCHEN(3, "【远微商城】"),
		/*** m10027 ***/
		M100027(4, "【m10027】"),
		/*** 云通讯 ***/
		YUNTONGXUN(5, "【云通讯】"),
		/*** 卡盟 ***/
		KAMENG(6, "【卡盟】");

		private int value;
		private String title;

		SmsType(int val, String title) {
			value = val;
			this.title = title;
		}

		private int getVal() {
			return value;
		}

		private String getTitle() {
			return title;
		}

		public static boolean contains(SmsType type) {
			for (SmsType val : SmsType.values()) {
				if (val.getVal() == type.getVal()) {
					return true;
				}
			}
			return false;
		}
	}

	public static boolean sendSms3(String recipient, final String content, SmsType type) {
		if(Constant.IS_TEST){
			log.info("测试环境，不发验证码");
			return true;
		}
		if (type == null || !SmsType.contains(type)) {
			log.info("短信接口调用来源渠道错误:" + type);
			return false;
		}
		final String msg = content.replace(" ", "");
		JSONObject data = new JSONObject();
		data.put("mobile", recipient);
		data.put("content", type.getTitle() + msg);
		String sendPost = HTTPUtils.sendPost(Constant.ORS_URL + "/s/message/sendMsg", data.toJSONString());
		JSONObject parse = JSONObject.parseObject(sendPost);
		return "200".equals(parse.getString("code"));
	}

	public static boolean sendMsg(String receiver, String content) {
		return sendSms3(receiver, content, SmsType.KAMENG);
	}

	public static void main(String[] args) {
		sendSms3("13990392582", "hello", SmsType.KAMENG);
	}

}
